#include "server.h"

extern MYSQL* mySql;

//判断路径名是否正确，返回正确文件名，正确目录名，错误路径名三种情况的标志，并通过传入传出参数获取该文件的id
int PathnameAnalyse(char* pathname, int userId, int* fileId) {
    char querySentence[1024] = {0};
    //分配一个指针数组，存储指向路径名中每一个文件名的指针
    char* name[100] = {0};
    //记录每一个分片的长度和分片的数量
    int spliceCount = 0, spliceLenth = 0;
    name[spliceCount] = (char*)calloc(100, sizeof(char));
    //开始分片
    for(int i = 0; i < (int)strlen(pathname); i++) {
        if(pathname[i] == '/') {
            spliceCount++;
            name[spliceCount] = (char*)calloc(100, sizeof(char));
            spliceLenth = 0;
        } else {
            name[spliceCount][spliceLenth] = pathname[i];
            spliceLenth++;
        }
    }
    //路径名以/结尾则最后一个分片为空，分片数量减一
    if(strlen(name[spliceCount]) == 0) {
        free(name[spliceCount]);
        name[spliceCount] = 0;
        spliceCount--;
    }
    //路径名以/开始则第一个分片为空，整体前移并把分片数量减一
    if(spliceCount >= 0) {
        if(strlen(name[0]) == 0) {
            for(int i = 0; i <= spliceCount; i++) {
                name[i] = name[i + 1];
            }
            free(name[spliceCount]);
            name[spliceCount] = 0;
            spliceCount--;
        }
    }
    //printf("pathname splice complete\n");
    //如果spliceCount小于0，说明传来的为空路径或仅为一个/，此时返回当前目录的id
    //获取当前工作路径，并把该工作路径分片，然后获得每个分片的id
    char cwd[1024] = {0};
    int ret = PwdDir(cwd, userId);
    //printf("cwd = %s\n", cwd);
    if(ret < 0) {
        //返回时释放堆空间
        for(int i = 0; i <= spliceCount; i++) {
            free(name[i]);
        }
        return WRONG_PATHNAME;
    }
    char* cwdName[100] = {0};
    int cwdSpliceCount = 0, cwdSpliceLenth = 0;
    cwdName[cwdSpliceCount] = (char*)calloc(100, sizeof(char));
    for(int i = 0; i < (int)strlen(cwd); i++) {
        if(cwd[i] == '/') {
            cwdSpliceCount++;
            cwdName[cwdSpliceCount] = (char*)calloc(100, sizeof(char));
            cwdSpliceLenth = 0;
        } else {
            cwdName[cwdSpliceCount][cwdSpliceLenth] = cwd[i];
            cwdSpliceLenth++;
        }
    }
    //从前往后判断路径正不正确，需要考虑.或..或~的情况
    int cwdPointer = cwdSpliceCount;
    int id[100] = {0};
    int flag = 0;
    if(spliceCount >= 0) {
        if(strcmp(name[0], "~") == 0) {
            cwdPointer = 0;
            flag = 1;
        }
    }
    //此处获取当前工作路径上所有文件的id
    //先获取~的id
    memset(querySentence, 0, sizeof(querySentence));
    sprintf(querySentence, "select id from filesystem where filename = '~' and owner_id = %d", userId);
    ret = mysql_query(mySql, querySentence);
    if(ret != 0) {
        for(int i = 0; i <= spliceCount; i++) {
            free(name[i]);
        }
        for(int i = 0; i <= cwdSpliceCount; i++) {
            free(cwdName[i]);
        }
        printf("query error: %s\n", mysql_error(mySql));
        return WRONG_PATHNAME;
    }
    MYSQL_RES* result = mysql_store_result(mySql);
    if(result == NULL) {
        for(int i = 0; i <= spliceCount; i++) {
            free(name[i]);
        }
        for(int i = 0; i <= cwdSpliceCount; i++) {
            free(cwdName[i]);
        }
        printf("query error: %s\n", mysql_error(mySql));
        return WRONG_PATHNAME;
    }
    MYSQL_ROW row = mysql_fetch_row(result);
    id[0] = atoi(row[0]);
    mysql_free_result(result);
    //再获取后续工作路径分片的id，如果spliceCount为-1，此处可以直接返回当前工作目录的id
    printf("spliceCount = %d, cwdSpliceCount = %d, cwdPointer = %d\n", spliceCount, cwdSpliceCount, cwdPointer);
    for(int i = 1; i <= cwdPointer; i++) {
        memset(querySentence, 0, sizeof(querySentence));
        sprintf(querySentence, 
                "select id from filesystem where parent_id = %d and owner_id = %d and filename = '%s' and type = 0", 
                id[i - 1], userId, cwdName[i]);
        ret = mysql_query(mySql, querySentence);
        if(ret != 0) {
            for(int i = 0; i <= spliceCount; i++) {
                free(name[i]);
            }
            for(int i = 0; i <= cwdSpliceCount; i++) {
                free(cwdName[i]);
            }
            printf("query error: %s\n", mysql_error(mySql));
            return WRONG_PATHNAME;
        }
        MYSQL_RES* result = mysql_store_result(mySql);
        if(result == NULL) {
            for(int i = 0; i <= spliceCount; i++) {
                free(name[i]);
            }
            for(int i = 0; i <= cwdSpliceCount; i++) {
                free(cwdName[i]);
            }
            printf("query error: %s\n", mysql_error(mySql));
            return WRONG_PATHNAME;
        }
        MYSQL_ROW row = mysql_fetch_row(result);
        id[i] = atoi(row[0]);
        mysql_free_result(result);
    }
    if(spliceCount < 0) {
        *fileId = id[cwdPointer];
        for(int i = 0; i <= spliceCount; i++) {
            free(name[i]);
        }
        for(int i = 0; i <= cwdSpliceCount; i++) {
            free(cwdName[i]);
        }
        return DIR_NAME;
    }
    //开始从前往后排查
    int type = 0;
    for(int i = flag; i <= spliceCount; i++) {
        if(strcmp(name[i], ".") == 0) {
            continue;
        } else if(strcmp(name[i], "..") == 0) {
            cwdPointer--;
            if(cwdPointer < 0) {
                for(int i = 0; i <= spliceCount; i++) {
                    free(name[i]);
                }
                for(int i = 0; i <= cwdSpliceCount; i++) {
                    free(cwdName[i]);
                }
                return WRONG_PATHNAME;
            }
        } else {
            cwdPointer++;
            if(cwdPointer > cwdSpliceCount) {
                cwdSpliceCount++;
                cwdName[cwdSpliceCount] = (char*)calloc(100, sizeof(char));
            }
            memset(querySentence, 0, sizeof(querySentence));
            sprintf(querySentence, "select id, filename, type from filesystem where parent_id = %d and filename = '%s'", 
                    id[cwdPointer - 1], name[i]);
            ret = mysql_query(mySql, querySentence);
            if(ret != 0) {
                for(int i = 0; i <= spliceCount; i++) {
                    free(name[i]);
                }
                for(int i = 0; i <= cwdSpliceCount; i++) {
                    free(cwdName[i]);
                }
                printf("query error: %s\n", mysql_error(mySql));
                return WRONG_PATHNAME;
            }
            MYSQL_RES* result = mysql_store_result(mySql);
            if(result == NULL) {
                for(int i = 0; i <= spliceCount; i++) {
                    free(name[i]);
                }
                for(int i = 0; i <= cwdSpliceCount; i++) {
                    free(cwdName[i]);
                }
                printf("query error: %s\n", mysql_error(mySql));
                return WRONG_PATHNAME;
            }
            MYSQL_ROW row = mysql_fetch_row(result);
            if(row == NULL) {
                for(int i = 0; i <= spliceCount; i++) {
                    free(name[i]);
                }
                for(int i = 0; i <= cwdSpliceCount; i++) {
                    free(cwdName[i]);
                }
                return WRONG_PATHNAME;
            }
            id[cwdPointer] = atoi(row[0]);
            memset(cwdName[cwdPointer], 0, 100);
            strcpy(cwdName[cwdPointer], row[1]);
            type = atoi(row[2]);
            mysql_free_result(result);
        }
    }
    printf("filetype = %d\n", type);
    *fileId = id[cwdPointer];
    if(type >= 1) {
        for(int i = 0; i <= spliceCount; i++) {
            free(name[i]);
        }
        for(int i = 0; i <= cwdSpliceCount; i++) {
            free(cwdName[i]);
        }
        return FILE_NAME;
    } else {
        for(int i = 0; i <= spliceCount; i++) {
            free(name[i]);
        }
        for(int i = 0; i <= cwdSpliceCount; i++) {
            free(cwdName[i]);
        }
        return DIR_NAME;
    }
    //到此处表明路径没问题，但是需要判断是普通文件还是目录文件
    //char filetype[10] = {0};
    //memset(querySentence, 0, sizeof(querySentence));
    //sprintf(querySentence, "select id, type from filesystem where parent_id = %d and filename = '%s'", 
    //        id[cwdPointer], name[spliceCount]);
    //ret = mysql_query(mySql, querySentence);
    //if(ret != 0) {
    //    for(int i = 0; i <= spliceCount; i++) {
    //        free(name[i]);
    //    }
    //    for(int i = 0; i <= cwdSpliceCount; i++) {
    //        free(cwdName[i]);
    //    }
    //    printf("query error: %s\n", mysql_error(mySql));
    //    return WRONG_PATHNAME;
    //}
    //result = mysql_store_result(mySql);
    //if(result == NULL) {
    //    for(int i = 0; i <= spliceCount; i++) {
    //        free(name[i]);
    //    }
    //    for(int i = 0; i <= cwdSpliceCount; i++) {
    //        free(cwdName[i]);
    //    }
    //    printf("query error: %s\n", mysql_error(mySql));
    //    return WRONG_PATHNAME;
    //}
    //row = mysql_fetch_row(result);
    //if(row == NULL) {
    //    for(int i = 0; i <= spliceCount; i++) {
    //        free(name[i]);
    //    }
    //    for(int i = 0; i <= cwdSpliceCount; i++) {
    //        free(cwdName[i]);
    //    }
    //    return WRONG_PATHNAME;
    //}
    //*fileId = atoi(row[0]);
    //strcpy(filetype, row[1]);
    //mysql_free_result(result);
    //if(strcmp(filetype, "1") == 0) {
    //    for(int i = 0; i <= spliceCount; i++) {
    //        free(name[i]);
    //    }
    //    for(int i = 0; i <= cwdSpliceCount; i++) {
    //        free(cwdName[i]);
    //    }
    //    return FILE_NAME;
    //} else {
    //    for(int i = 0; i <= spliceCount; i++) {
    //        free(name[i]);
    //    }
    //    for(int i = 0; i <= cwdSpliceCount; i++) {
    //        free(cwdName[i]);
    //    }
    //    return DIR_NAME;
    //}
}
